package com.lhys.controller;


import com.github.pagehelper.PageInfo;
import com.lhys.common.ApiResultMsg;
import com.lhys.common.ResultInfo;
import com.lhys.controller.base.BaseController;
import com.lhys.pojo.AssetProviderType;
import com.lhys.service.AssetProviderTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 资产_供应商_类型表 前端控制器
 * </p>
 *
 * @author 小七
 * @since 2021-05-06
 */
@Api(tags = "供应商类型模块")
@RestController
@RequestMapping("/api/asset-provider-type")
public class AssetProviderTypeController extends BaseController {
    @Autowired
    private AssetProviderTypeService assetProviderTypeService;

    @PutMapping("/save")
    @ApiOperation(value = "供应商类型添加或更新", authorizations = {@Authorization(value = header)})
    public ApiResultMsg saveAssetProviderType(@RequestBody AssetProviderType assetProviderType) {
        Boolean aBoolean = assetProviderTypeService.saveAssetProviderType(assetProviderType);
        if(aBoolean == null){
            return ApiResultMsg.error("该供应商类型标题或编码已存在");
        }
        if (!aBoolean) {
            return ApiResultMsg.error(ResultInfo.ERROR);
        }
        return ApiResultMsg.success(ResultInfo.SUCCESS);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据id供应商类型删除", authorizations = {@Authorization(value = header)})
    public ApiResultMsg deleteAssetProviderType(@PathVariable("id") Integer id) {
        Boolean aBoolean = assetProviderTypeService.delAssetProviderTypeById(id);
        if (!aBoolean) {
            return ApiResultMsg.error(ResultInfo.ERROR);
        }
        return ApiResultMsg.success(ResultInfo.SUCCESS);
    }

    @DeleteMapping("/delete/{title}")
    @ApiOperation(value = "根据title供应商类型删除", authorizations = {@Authorization(value = header)})
    public ApiResultMsg deleteAssetProviderType(@PathVariable("title") String title) {
        Boolean aBoolean = assetProviderTypeService.delAssetProviderTypeByTitle(title);
        if (!aBoolean) {
            return ApiResultMsg.error(ResultInfo.ERROR);
        }
        return ApiResultMsg.success(ResultInfo.SUCCESS);
    }

    @GetMapping("/getAssetProviderType/{id}")
    @ApiOperation(value = "根据id查询供应商类型", authorizations = {@Authorization(value = header)})
    public ApiResultMsg getAssetProviderTypeById(@PathVariable("id") Integer id) {
        AssetProviderType assetProviderType = assetProviderTypeService.getAssetProviderTypeById(id);
        return ApiResultMsg.success(ResultInfo.SUCCESS, assetProviderType);
    }

    @GetMapping("/getAssetProviderType/{title}")
    @ApiOperation(value = "根据title查询供应商类型", authorizations = {@Authorization(value = header)})
    public ApiResultMsg getAssetProviderTypeByTitle(@PathVariable("title") String title) {
        AssetProviderType assetProviderType = assetProviderTypeService.getAssetProviderTypeByTitle(title);
        return ApiResultMsg.success(ResultInfo.SUCCESS, assetProviderType);
    }

    @GetMapping("/list")
    @ApiOperation(value = "供应商类型查询", authorizations = {@Authorization(value = header)})
    public ApiResultMsg listAssetProviderType(AssetProviderType assetProviderType) {
        List<AssetProviderType> assetProviderTypeList = assetProviderTypeService.getAssetProviderTypeList();
        return ApiResultMsg.success(ResultInfo.SUCCESS, assetProviderTypeList);
    }

    @GetMapping("/pageList")
    @ApiOperation(value = "供应商类型查询分页", authorizations = {@Authorization(value = header)})
    public ApiResultMsg listAssetProviderTypeByPage(AssetProviderType assetProviderType, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<AssetProviderType> assetProviderTypeList = assetProviderTypeService.getAssetProviderTypeList(pageNum, pageSize);
        return ApiResultMsg.success(ResultInfo.SUCCESS, assetProviderTypeList);
    }
}

